Replacing multiple `-` with one `-` without regexp

Posted by Adam Kiss on Stack Overflow See other posts from Stack Overflow or by Adam Kiss
Published on 2010-04-02T14:37:34Z Indexed on 2010/04/02 14:43 UTC
Read the original article Hit count: 126

Filed under:
|

I've seen so many misuses of RegExp, I don't really like it :)

I have string (as a result of two str_replaces) that might look something like this:

.?This iš my ".stRiNg."!
          |
          V
--this-is-my---string---

Is there any way better than

$string = trim(preg_replace('/[-]+/u','-', $string),'-');

to get:

this-is-my-string

?

© Stack Overflow or respective owner

Related posts about php

Related posts about regex